# 第一阶段：构建前端
FROM nginx:alpine AS frontend

# 清理默认配置并添加自定义配置
RUN rm -rf /etc/nginx/conf.d/*
COPY bg-print-ui/default.conf /etc/nginx/conf.d/

# 复制前端构建产物
COPY bg-print-ui/dist/ /usr/share/nginx/html/

# 第二阶段：构建后端
FROM openjdk:17-alpine AS backend

# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

# 复制JAR包到标准路径
COPY bg-print-java/target/*.jar /app.jar

# 最终阶段：合并运行环境
FROM alpine:3.18

# 安装运行依赖
RUN apk add --no-cache \
    openjdk17-jre-headless \
    nginx \
    tzdata && \
    # 设置时区
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone && \
    # 创建日志目录
    mkdir -p /var/log/nginx && \
    # 清理默认配置
    rm -rf /etc/nginx/conf.d/*

# 从前端阶段复制文件
COPY --from=frontend /usr/share/nginx/html/ /usr/share/nginx/html/
COPY --from=frontend /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/

# 从后端阶段复制JAR包
COPY --from=backend /app.jar /app.jar

# 暴露端口
EXPOSE 80 5000

# 启动脚本
CMD (nginx -g "daemon off;" &) && \
    java -Dfile.encoding=utf8 \
         -Djava.security.egd=file:/dev/./urandom \
         -jar /app.jar \
         --server.port=5000